package cn.cyx.demo.chat.model;

import io.netty.channel.Channel;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;

import java.time.LocalDateTime;
import java.util.Objects;

/**
 * 用户会话数据模型
 * 
 * @author cyx
 * @since 1.0.0
 */
public class UserSession {
    
    /** 用户ID */
    private String userId;
    
    /** 用户名 */
    private String username;
    
    /** 用户昵称 */
    private String nickname;
    
    /** 用户头像 */
    private String avatar;
    
    /** 当前房间ID */
    private String roomId;
    
    /** 用户连接通道 */
    private Channel channel;
    
    /** 最后活跃时间 */
    private LocalDateTime lastActiveTime;
    
    /** 用户状态 */
    private UserStatus status;
    
    /**
     * 用户状态枚举
     */
    public enum UserStatus {
        /** 在线 */
        ONLINE,
        /** 离线 */
        OFFLINE,
        /** 忙碌 */
        BUSY
    }
    
    public UserSession() {
        this.lastActiveTime = LocalDateTime.now();
        this.status = UserStatus.ONLINE;
    }
    
    public UserSession(String userId, String username, Channel channel) {
        this();
        this.userId = userId;
        this.username = username;
        this.channel = channel;
    }
    
    /**
     * 发送消息给用户
     * 
     * @param message 消息内容
     */
    public void sendMessage(String message) {
        if (channel != null && channel.isActive()) {
            channel.writeAndFlush(new TextWebSocketFrame(message));
        }
    }
    
    /**
     * 更新最后活跃时间
     */
    public void updateLastActiveTime() {
        this.lastActiveTime = LocalDateTime.now();
    }
    
    /**
     * 检查用户是否在线
     * 
     * @return 是否在线
     */
    public boolean isOnline() {
        return channel != null && channel.isActive() && status == UserStatus.ONLINE;
    }
    
    // Getters and Setters
    public String getUserId() {
        return userId;
    }
    
    public void setUserId(String userId) {
        this.userId = userId;
    }
    
    public String getUsername() {
        return username;
    }
    
    public void setUsername(String username) {
        this.username = username;
    }
    
    public String getNickname() {
        return nickname;
    }
    
    public void setNickname(String nickname) {
        this.nickname = nickname;
    }
    
    public String getAvatar() {
        return avatar;
    }
    
    public void setAvatar(String avatar) {
        this.avatar = avatar;
    }
    
    public String getRoomId() {
        return roomId;
    }
    
    public void setRoomId(String roomId) {
        this.roomId = roomId;
    }
    
    public Channel getChannel() {
        return channel;
    }
    
    public void setChannel(Channel channel) {
        this.channel = channel;
    }
    
    public LocalDateTime getLastActiveTime() {
        return lastActiveTime;
    }
    
    public void setLastActiveTime(LocalDateTime lastActiveTime) {
        this.lastActiveTime = lastActiveTime;
    }
    
    public UserStatus getStatus() {
        return status;
    }
    
    public void setStatus(UserStatus status) {
        this.status = status;
    }
    
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        UserSession that = (UserSession) o;
        return Objects.equals(userId, that.userId);
    }
    
    @Override
    public int hashCode() {
        return Objects.hash(userId);
    }
    
    @Override
    public String toString() {
        return "UserSession{" +
                "userId='" + userId + '\'' +
                ", username='" + username + '\'' +
                ", nickname='" + nickname + '\'' +
                ", roomId='" + roomId + '\'' +
                ", lastActiveTime=" + lastActiveTime +
                ", status=" + status +
                '}';
    }
} 